// JavaScript Document

////////////////////////////////////////////////////////////////////////////////////
//ws
PJS.ws = function(){
	this.uid = PJS.rand("pjs_xc");
	this.depth = 0;
	this.response_errors = ["400","401","402","403","404","500","501","502","503"];
	
	this._uri = "ws.php";
	this._mpf = "ws_";
};
PJS.ws.prototype.call = function(m,p){
	xcb(this._uri,m,p);
};
PJS.ws.prototype.getForm = function(form){
	if(typeof(form) == "string"){
		form = document.forms[form];	
	}
	if(form && form.tagName && form.tagName == 'FORM'){
		return form	;
	}
	return null;
};
PJS.ws.prototype.getFormValues = function(frm){
	var objForm;
	var submitDisabledElements = false;
	if(arguments.length > 1 && arguments[1] == true)
		submitDisabledElements = true;
	var prefix="";
	if(arguments.length > 2)
		prefix = arguments[2];
	
	if(typeof(frm) == "string")
		objForm = PJS.$E(frm);
	else
		objForm = frm;
	var sXml = "";
	if(objForm && objForm.tagName == 'FORM')
	{
		var formElements = objForm.elements;
		for( var i=0; i<formElements.length; i++)
		{
			if(!formElements[i].name)
				continue;
			if(formElements[i].name.substring(0,prefix.length) != prefix)
				continue;
			if(formElements[i].type && (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && formElements[i].checked == false)
				continue;
			if(formElements[i].disabled && formElements[i].disabled == true && submitDisabledElements == false)
				continue;
			var name = formElements[i].name;
			if(name)
			{
				if(sXml != '')
					sXml += '&';
				if(formElements[i].type=='select-multiple')
				{
					for(var j = 0; j<formElements[i].length; j++)
					{
						if(formElements[i].options[j].selected == true)
							sXml += name+"="+encodeURIComponent(formElements[i].options[j].value)+"&";
					}
				}
				else
				{
					sXml += name+"="+encodeURIComponent(formElements[i].value);
				}
			} 
		}
	}
	
	sXml +="";
	
	return sXml;
};
PJS.ws.prototype.postForm = function(form,m){
	var form = this.getForm(form);
	if(form){
		if(form.getAttribute("id")==null){
			form.setAttribute("id",PJS.rand("form"));	
		}
		var fvs = xcgfm(form.getAttribute("id"));
		if(typeof(m)=="undefined"){
			m = "home";
		}
		m = this._mpf + m;
		this.call(m,[fvs,form.getAttribute("id")]);
	}
};
PJS.ws.prototype.postFormC = function(form){
	this._uric = this._uri;
	this._uri = document.location.href;
	this.postForm(form);
	this._uri = this._uric;
};
PJS.ws.prototype.postFormCB = function(r){
	if(r.status==1){
		alert("OK");	
		if(r.f && $E(r.f)){
			$E(r.f).reset();
		}
		this.postFormHandler(r);
	} else {
		alert("Error");	
	}
};
PJS.ws.prototype.postFormHandler = function(r){};
PJS.ws.prototype.get = function(m,args){
	this.call(m,args);
};
PJS.ws.prototype.widget = function(args){
	this.call('widget',args);
};
//
var pjs_ws = new PJS.ws();
//